package org.example.tlias.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.tlias.mapper.EmpExprMapper;
import org.example.tlias.mapper.EmpMapper;
import org.example.tlias.pojo.*;
import org.example.tlias.services.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    EmpMapper empMapper;
    @Autowired
    EmpExprMapper empExprMapper;
    //原始分页查询操作
  /*  @Override
    public PageResult page(Integer page, Integer pageSize) {
        Long total = empMapper.count();
        int start = (page - 1) * pageSize;
        List<Emp> rows = empMapper.page(start, pageSize);
        return new PageResult<Emp>(total,rows);
    }*/

    /**
     *借助分页查询插件pageHelper实现分页查询
     * @param page
     * @param pageSize
     * @return
     */
 /*   @Override
    public PageResult page(Integer page, Integer pageSize) {
        //开启分页
        PageHelper.startPage(page,pageSize);
        //查询所有
        List<Emp> empList = empMapper.page1();
        //封装分页结果
        Page<Emp> empPage = (Page<Emp>) empList;

        return new PageResult<Emp>(empPage.getTotal(),empPage.getResult());
    }*/
  /*  @Override
    public PageResult page(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
        PageHelper.startPage(page,pageSize);
        List<Emp> empList = empMapper.page2(name,gender,begin,end);
        Page<Emp> empPage = (Page<Emp>) empList;
        return new PageResult<Emp>(empPage.getTotal(),empPage.getResult());
    }*/
    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
//        List<Emp> empList = empMapper.page2(empQueryParam.getName(), empQueryParam.getGender(), empQueryParam.getBegin(), empQueryParam.getEnd());
        List<Emp> empList = empMapper.page3(empQueryParam);
        Page<Emp> empPage = (Page<Emp>) empList;
        return new PageResult<Emp>(empPage.getTotal(),empPage.getResult());
    }


    @Transactional(rollbackFor = {Exception.class},propagation = Propagation.REQUIRED)//默认是运行时抛出异常才会回滚,rollbackFor指定什么异常回滚
    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            empExprMapper.saveBatch(exprList);
        }
    }
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(List<Integer> ids) {
        //1.删除员工
        empMapper.deleteByIds(ids);
        //2.删除员工对应的工作经历
        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public Emp getById(Integer id) {
        Emp emp = empMapper.getById(id);
        return emp;
    }

    @Override
    public void update(Emp emp) {
        //1.更新员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
        //2.删除员工对应的工作经历
            empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        //3.添加员工对应的工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            empExprMapper.saveBatch(exprList);
        }
    }

    @Override
    public List<Emp> list() {
        return empMapper.list();
    }

    @Override
    public LoginInfo login(Emp emp) {
        //1.调用mapper接口，根据用户名和密码查询员工
        Emp emp1 = empMapper.selectByUsernameAndPassword(emp);
        //2.判断：判断是否存在这个员工，组装登录成功信息
        if(emp1 != null){
            LoginInfo info = new LoginInfo();
            info.setId(emp1.getId());
            info.setName(emp1.getName());
            info.setUsername(emp1.getUsername());
            return info;
//            info.setToken(TokenUtils.genToken(emp1.getId().toString(),emp1.getPassword()));
        }
        //3.不存在，返回null
        return null;
    }

}
